﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Facet.Combinatorics;

namespace Solver
{
    // jedna Kombinace karet
    public class SolutionCombination : List<OneCard>
    {        

        public SolutionCombination()
        {
            ID = 0;
        }

        public void Add(OneCard c)
        {
            base.Add(c);
        }

        public OneCard this[int idx]
        {
            get
            {                
                return base[idx];
            }
            set
            {
                if (value == null)
                {
                    int a = 0;
                }
                base[idx] = value;
            }
        }

        /// <summary>
        /// Vraci jen kombinaci karet v int
        /// </summary>
        /// <returns></returns>
        public int[] GetCardsCombination()
        {
            List<int> list = new List<int>();
            foreach (OneCard c in this)
                list.Add(c.ID);

            return list.ToArray();
        }

        // kopirovaci konstruktor
        public SolutionCombination(SolutionCombination st)
        {
            foreach (OneCard c in st)
            {
                OneCard new_c = new OneCard(c);
                Debug.Assert(new_c != null);
                Add(new_c);
            }
        }
        
        // nacst karty ze stringu
        public void loadSolution(string data)
        {
            Clear();
            string[] array = data.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

            foreach (string s in array)
            {
                OneCard card = new OneCard();
                card.ReadCard(s);
                Debug.Assert(card != null);
                Add(card);
            }
        }

        public int ID
        {
            get;
            set;
        }

        OneCard[,] _solution = new OneCard[3,3];
        public bool CheckSolution()
        {
            int idx = 0;
            foreach (OneCard c in this)            
            {
                if (c == null)
                    return false;

                int x = idx % 3;
                int y = idx / 3;
                _solution[y,x] = c;
                idx++;
            }

            int score = CheckSolution(_solution);
            Sucess = (score == 0);
            return Sucess; // pokud jsou vsechny soucty solutionu 0 = success
        }

        public int CheckSolution(OneCard[,] solution)
        {
            int score = 0;
            for (int i = 0; i < 3; i++)
            {
                OneCard c1 = solution[i, 0];
                OneCard c2 = solution[i, 1];
                int s1 = Math.Abs(c1.Right.Value + c2.Left.Value);
                score += s1;
                OneCard c3 = solution[i, 2];
                int s2 = Math.Abs(c2.Right.Value + c3.Left.Value);
                score += s2;
            }

            for (int i = 0; i < 3; i++)
            {
                OneCard c1 = solution[0, i];
                OneCard c2 = solution[1, i];
                int s1 = Math.Abs(c1.Bottom.Value + c2.Top.Value);
                score += s1;
                OneCard c3 = solution[2, i];
                int s2 = Math.Abs(c2.Bottom.Value + c3.Top.Value);
                score += s2;
            }

            return score;
        }

        private bool _success = false;
        public bool Sucess
        {
            get {return _success;}
            set {_success = value;}
        }

        public override string ToString()
        {
            string line = "";
            bool first = true;
            foreach (OneCard c in this)
            {

                if (first == true)
                {
                    if (c == null)
                        line += "[null]";
                    else
                        line += "(" + c.ID + ")" + "[" + c.ToString() + "]";
                }
                else
                {
                    if (c == null)
                        line += ",[null]";
                    else
                        line += ",(" + c.ID + ")" + "[" + c.ToString() + "]";
                }

            }

            return line;
        }
    }
}
